<?php

namespace addons\yungift\model;

use addons\yungift\library\ModelTrait;
use think\Model;
use traits\model\SoftDelete;

/**
 * 意见反馈模型
 */
class Feedback extends Model
{
    use ModelTrait;
    use SoftDelete;

    // 表名,不含前缀
    protected $name = 'yungift_feedback';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $deleteTime = 'deleted_at';
    protected $updateTime = 'updated_at';
    protected $createTime = 'created_at';

    // 不显示的字段
    protected $hidden = ['deleted_at'];

    protected $resultSetType = 'collection';

    // 活动数据
    public function activityData()
    {
        return $this->belongsTo(Activity::class, 'activity_id', 'id');
    }

    // 成员数据
    public function memberData()
    {
        return $this->belongsTo(Member::class, 'member_id', 'id');
    }

    // 类型
    const TYPE_1 = 1;
    const TYPE_2 = 2;

    public static function getTypeList()
    {
        return [self::TYPE_1 => __('数据问题'), self::TYPE_2 => __('软件使用')];
    }

    public function getTypeTextAttr($value, $data)
    {
        $optionList = self::getTypeList();

        return $optionList[$data['type']] ?? $data['type'];
    }

    // 图片列表
    public function getPhotoListAttr($value, $data)
    {
        $photoList = [];
        if ($photos = $data['photos']) {
            $photos = explode(',', $photos);
            foreach ($photos as $item) {
                $photoList[] = ['src' => $item, 'url' => cdnurl($item, true)];
            }
        }

        return $photoList;
    }

    // 回复图片列表
    public function getReplyPhotoListAttr($value, $data)
    {
        $photoList = [];
        if ($photos = $data['reply_photos']) {
            $photos = explode(',', $photos);
            foreach ($photos as $item) {
                $photoList[] = ['src' => $item, 'url' => cdnurl($item, true)];
            }
        }

        return $photoList;
    }

    // 状态
    const STATUS_NORMAL = 1;
    const STATUS_HIDDEN = 2;

    public static function getStatusList()
    {
        return [self::STATUS_NORMAL => __('待回复'), self::STATUS_HIDDEN => __('已回复')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $optionList = self::getStatusList();

        return $optionList[$data['status']] ?? $data['status'];
    }
}
